{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "03277434",
   "metadata": {},
   "source": [
    "## 易错点\n",
    "1. df_X = df_cluster.select_dtypes(include=np.number) 忘记了,好在通过帮助快捷键找到了这个方法\n",
    "2. 轮廓系数为:silhouette_score\n",
    "3. 绘制折线图用plt自带就行plt.plot(range(2,20),silhouette,'*')\n",
    "4. s2 = np.sum(np.linalg.norm(cent[0]-cent[1],axis=0)) 忘了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd5aadc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# importing the necessary packages\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "from sklearn import metrics\n",
    "sns.set()\n",
    "pd.options.display.max_columns = None\n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)\n",
    "origin_data=pd.read_csv('./data/data_cluster.csv')\n",
    "origin_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c070edec",
   "metadata": {},
   "source": [
    "1.使用 sample()方法从 origin_data 中随机抽取了10000个样本，并允许重复抽样。重置索引，使得输出 data 为以下格式:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e5776dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "data = origin_data.sample(10000)\n",
    "#由考生填写\n",
    "data.reset_index()\n",
    "print(data.shape)\n",
    "df_cluster = data.copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc27a31",
   "metadata": {},
   "source": [
    "2.从 df_cluster 中选择所有 np.number 类型的列，并将其保存在df_X中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c59be744",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "df_X = df_cluster.select_dtypes(include=np.number)\n",
    "#由考生填写\n",
    "cols = df_X.columns\n",
    "cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26ca5966",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler()\n",
    "scaled = pd.DataFrame(scaler.fit_transform(df_X))\n",
    "scaled.columns = cols\n",
    "scaled.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6464388e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "from sklearn.metrics import silhouette_score\n",
    "K,Silhouette,SSE = [],[],[]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a05de27",
   "metadata": {},
   "source": [
    "3.使用 K-Means 模型，设置聚类数为i，初始化算法为k-means++，最大迭代次数为 500，初始化次数为 10，随机种子为 0，并将模型拟合到scaled 数据上。计算使用欧几里得距离度量标准的轮廓系数s1 和模型的质心间距离的总和 s2。输出如下所示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4e2fbff1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "from sklearn.metrics import silhouette_score\n",
    "for i in range(2,20):\n",
    "    k_model = KMeans(n_clusters=i,init='k-means++',max_iter=500,n_init=10,random_state=0)\n",
    "    k_model.fit(scaled)\n",
    "    label = k_model.labels_\n",
    "    s1 = silhouette_score(X=scaled,labels=label)\n",
    "    sse = k_model.inertia_\n",
    "    cent = k_model.cluster_centers_\n",
    "    # np.linalg.norm是NumPy库中的一个函数，用于计算向量或矩阵的范数。\n",
    "    # 范数是一种衡量向量或矩阵大小的度量方式，在线性代数中有广泛的应用\n",
    "    s2 = np.sum(np.linalg.norm(cent[0]-cent[1],axis=0))\n",
    "    k.append(i)\n",
    "    silhouette.append(s1)\n",
    "    sses.append(sse)\n",
    "    print('K=',i,'Silhouette =',s1,'SSE =',sse,'distances=',s2)\n",
    "#由考生填写\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f9d7285",
   "metadata": {},
   "source": [
    "轮廓系数，sse折线图： https://zhuanlan.zhihu.com/p/51777857"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e900fe8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "plt.figure()\n",
    "plt.title('K-means silhouette')\n",
    "plt.plot(range(2,20),silhouette,'*')\n",
    "plt.plot(range(2,20),silhouette ,'-',alpha=0.5) \n",
    "plt.xlabel('Number of Cluster')\n",
    "plt.ylabel('Silhouette')\n",
    "plt.show()\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8e66834",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "plt.figure()\n",
    "plt.title('K-means SSE')\n",
    "plt.xlabel('Number of cluster')\n",
    "plt.ylabel('SSE')\n",
    "plt.plot(np.array(range(2,20)),sses,'o')\n",
    "plt.plot(np.array(range(2,20)),sses,'-',alpha=0.5)\n",
    "plt.show()\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f65ed4b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark-2.4.5",
   "language": "python",
   "name": "pyspark-2.4.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
